﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CApC3DSnapshotApp
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands;

internal class CApC3DSnapshotApp
{
  private CApC3DSnapshotNodeData m_currentSnapshot;

  public CApC3DSnapshotNodeData CurrentSnapshot => this.m_currentSnapshot;

  internal TxObjectList GetAllSnapshots()
  {
    TxObjectList txObjectList = new TxObjectList();
    return TxApplication.ActiveDocument.PhysicalRoot.GetDirectDescendants((ITxTypeFilter) new TxTypeFilter(typeof (TxSnapshot)));
  }

  internal void CreateCurrentSnapshot()
  {
    CApC3DUtils.GetResourceManager().GetString("CREATE_3D_DOCUMENTATION_CURRENT_VIEW");
    this.m_currentSnapshot = new CApC3DSnapshotNodeData(SnapshotType.CurrentView);
  }

  internal void RestoreCurrentSnapshot() => this.RestoreCurrentSnapshot(true);

  internal void RestoreCurrentSnapshot(bool applyObjVisibility)
  {
    if (this.m_currentSnapshot == null)
      return;
    this.m_currentSnapshot.Snapshot.Apply(new TxApplySnapshotParams()
    {
      ObjectsLocation = true,
      ObjectsVisibility = applyObjVisibility,
      PointOfView = true,
      DevicePoses = true,
      ObjectsAttachments = true,
      ObjectsColor = true,
      ObjectsViewingMode = true,
      PMIsScale = true
    });
  }

  internal void SetSnapshot(CApC3DProcessNodeData processNode)
  {
    if (processNode.AttachedSnapshotData.IsCurrentView)
    {
      processNode.AttachedSnapshotData.Snapshot = this.m_currentSnapshot.Snapshot;
    }
    else
    {
      if (!processNode.AttachedSnapshotData.IsStartOperationView)
        return;
      processNode.AttachedSnapshotData.CreateStartOperationView(processNode);
    }
  }

  internal void DeleteTempSnapshots(CApC3DProcessNodeData root)
  {
    if (this.m_currentSnapshot != null)
    {
      this.m_currentSnapshot.Snapshot.Delete();
      this.m_currentSnapshot = (CApC3DSnapshotNodeData) null;
    }
    this.DeleteStartOperationSnapshots(root);
  }

  private void DeleteStartOperationSnapshots(CApC3DProcessNodeData operation)
  {
    if (operation.IsStep && operation.AttachedSnapshotData.IsStartOperationView)
      operation.AttachedSnapshotData.DeleteStartOperationView();
    foreach (CApC3DProcessNodeData child in operation.GetChildren(true))
      this.DeleteStartOperationSnapshots(child);
  }

  internal void CreateDataFile(
    CApC3DProcessNodeData rootNode,
    TxExportGraphicsAnimationOutputData animationOutputData,
    string dataFile,
    CApC3DDocumentationSettings documentationSettings,
    CApC3DAppearanceSet appearanceSet)
  {
    CApDataFileWriter writer = new CApDataFileWriter(dataFile);
    writer.Initialize(animationOutputData, documentationSettings, appearanceSet);
    writer.SetDocumentationSettings(documentationSettings);
    this.AddView(writer, this.m_currentSnapshot);
    writer.AddAllObjects(documentationSettings.PdfMode);
    this.AddStep(writer, rootNode);
    if (documentationSettings.StoreAllSnapshots)
    {
      foreach (TxSnapshot allSnapshot in (Collection<ITxObject>) this.GetAllSnapshots())
      {
        CApC3DSnapshotNodeData snapshotData = new CApC3DSnapshotNodeData(allSnapshot);
        this.AddView(writer, snapshotData);
      }
    }
    writer.Close();
  }

  internal void CreateNodesFile(
    CApC3DProcessNodeData rootNode,
    TxExportGraphicsAnimationOutputData animationOutputData,
    string nodesFile,
    CApC3DDocumentationSettings documentationSettings)
  {
    CApNodesFileWriter capNodesFileWriter = new CApNodesFileWriter(nodesFile);
    capNodesFileWriter.Initialize(animationOutputData, documentationSettings);
    capNodesFileWriter.AddAllNodes(documentationSettings.PdfMode);
    capNodesFileWriter.Close();
  }

  private void AddStep(CApDataFileWriter writer, CApC3DProcessNodeData operation)
  {
    if (operation.IsStep)
    {
      TxSnapshot snapshot = operation.AttachedSnapshotData.Snapshot;
      writer.AddStep((ITxObject) operation.NodeObject, snapshot);
      this.AddView(writer, operation.AttachedSnapshotData);
    }
    List<CApC3DProcessNodeData> children = operation.GetChildren(true);
    int count = children.Count;
    for (int index = 0; index < count; ++index)
    {
      CApC3DProcessNodeData operation1 = children[index];
      this.AddStep(writer, operation1);
    }
  }

  private void AddView(CApDataFileWriter writer, CApC3DSnapshotNodeData snapshotData)
  {
    if (snapshotData == null || !TxSnapshot.op_Inequality(snapshotData.Snapshot, (ITxObject) null))
      return;
    snapshotData.Snapshot.Apply(new TxApplySnapshotParams()
    {
      ObjectsLocation = true,
      ObjectsVisibility = true,
      PointOfView = true,
      DevicePoses = true,
      ObjectsAttachments = true,
      ObjectsColor = true,
      ObjectsViewingMode = true
    });
    writer.AddView(snapshotData);
  }
}
